local skill = fk.CreateSkill {
  name = "lvshi__zhiyu",
}

Fk:loadTranslationTable{
  ["lvshi__zhiyu"] = "智愚",
  [":lvshi__zhiyu"] = "当你受到1点伤害后，你可令两名角色拼点，拼点赢的角色可令你回复1点体力，否则你重置“奇策”的一个牌名。",
  ["#lvshi__qice_reset"] = "智愚",
  ["#lvshi__zhiyu-choose"] = "智愚：你可令两名角色拼点",
  ["#lvshi__zhiyu-recover"] = "智愚：你可令%src回复1点体力",
  ["#zhiyu-reset"] = "智愚：你可令%src重置“奇策”",

  ["$lvshi__zhiyu1"] = "二袁相争，此曹公得利之时。",
  ["$lvshi__zhiyu2"] = "穷寇宜追，需防死蛇之不僵。",
}

skill:addEffect(fk.Damaged, {
  anim_type = "masochism",
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 2,
      max_num = 2,
      targets = table.filter(room.alive_players, function (p)
        return not p:isKongcheng()
      end),
      skill_name = skill.name,
      prompt = "#lvshi__zhiyu-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local target1 = event:getCostData(self).tos[1]
    local target2 = event:getCostData(self).tos[2]
    local pindian = target1:pindian({target2}, self.name)
      local recover = false
      if pindian.results[target2].winner then
        local winner
        if pindian.results[target2].winner == target1 then
          winner = target1
        else
          winner = target2
        end
        if not winner.dead then
          if room:askToSkillInvoke(winner, {
            skill_name = skill.name,
            prompt = "#lvshi__zhiyu-recover:"..player.id}) then
            recover = true
            room:recover({
              who = player,
              num = 1,
              recoverBy = player,
              skillName = skill.name
            })
          end
        end
      end
      if not recover then
        local qicenames = player:getTableMark("qice_names")
        if #qicenames > 0 then
          local choice = room:askToChoice(player,{
            choices = qicenames,
            skill_name = skill.name,
            prompt = "#lvshi__qice_reset"
          })
          table.removeOne(qicenames, choice)
        end
        room:setPlayerMark(player, "qice_names", qicenames)
      end
  end,
})

return skill